<!--添加修改角色信息-->
<template>
  <a-modal v-model="showAddTable" :mask-closable="false" :closable="false" :width="700"
           title='排课'
           :maskClosable="false"
           :destroyOnClose="true">
    <a-form :form="form" @keydown.native.enter.prevent="saveApply" class="modelSearch">
      <a-form-item label="排课开始日期：" v-bind="formStyle" required>
        <a-range-picker style="width: 435px" v-model="dateRange"
                        v-decorator="['dateRange', { rules: [{ required: true, message: '选择上课日期' }] }]"
        />
      </a-form-item>
      <a-form-item label="周几上课：" v-bind="formStyle" required>
        <a-checkbox-group v-model="weekList"
                          v-decorator="['weekList', { rules: [{ required: true, message: '选择上课周数' }] }]"
        >
          <a-row colspan="25" style="width: 500px">
            <a-col :span="5">
              <a-checkbox :value="Number('1')">周一</a-checkbox>
            </a-col>
            <a-col :span="5">
              <a-checkbox :value="Number('2')">周二</a-checkbox>
            </a-col>
            <a-col :span="5">
              <a-checkbox :value="Number('3')">周三</a-checkbox>
            </a-col>
            <a-col :span="5">
              <a-checkbox :value="Number('4')">周四</a-checkbox>
            </a-col>
            <a-col :span="5">
              <a-checkbox :value="Number('5')">周五</a-checkbox>
            </a-col>
            <a-col :span="5">
              <a-checkbox :value="Number('6')">周六</a-checkbox>
            </a-col>
            <a-col :span="5">
              <a-checkbox :value="Number('7')">周日</a-checkbox>
            </a-col>
          </a-row>
        </a-checkbox-group>
      </a-form-item>
    </a-form>
    <div slot="footer">
      <a-button type="default" size="default" @click="cancelApply()">取消</a-button>
      <a-button type="primary" size="default" @click="saveApply()">确定</a-button>
    </div>
    <div class="spinBox" v-if="spinning">{{spinText}}</div>
  </a-modal>
</template>

<script>
import verify from 'libs/verify'
import AFormItem from 'ant-design-vue/es/form/FormItem'
import moment from "moment";

export default {
  name: 'schedule',
  components: {
    AFormItem,
    verify
  },
  data() {
    return {
      verify,
      form: this.$form.createForm(this),
      showAddTable: false,
      name: '',
      classId: '',
      startDate: '',
      endDate: '',
      attendTime: '',
      finishTime: '',
      spinning:false,
      spinText:'请稍后...',
      weekList: [],
      dateRange: [],
    }
  },
  computed: {
    classSelectList() {
      return this.$store.state.courseManage.classSelectList
    },
    formStyle() {
      return this.$store.state.common.formStyle
    }
  },
  props: {

    courseObj: {
      type: Object,
      required: true
    },
  },
  watch: {},
  methods: {

    changeAttendTime(time,timeString){
      console.log(time)
      console.log(timeString)
    },
    /**
     * Function    : saveApply
     * Description : 打开弹框
     * Input       : none
     * Return      : none
     */
    showModal() {
      // this.courseId = courseId
      this.showAddTable = true
    },
    /**
     * Function    : disabledEndDate
     * Description : 对结束时间进行限制
     * Input       : endValue
     * Return      : None
     */
    disabledEndDate ( endValue ) {
      const startDate = this.startDate
      if (startDate) {
        return startDate.valueOf () >= endValue.valueOf ()
      }
      return (
          endValue && endValue < moment().subtract(1, 'days')
      )
    },

    /**
     * Function    : saveApply
     * Description : 角色信息新增、编辑保存
     * Input       : name
     * Return      : none
     */
    saveApply() {
      let _this = this
      this.form.validateFields((err, value) => {
        if (!err) {
          let para = {}
          para.courseList = [
            this.courseObj.id,
          ]
          _this.spinning = true
          para.startDate = moment(this.dateRange[0]).format("YYYY-MM-DD")
          para.endDate = moment(this.dateRange[1]).format("YYYY-MM-DD")
          para.weekList = this.weekList
          _this.$store.dispatch('batchSaveScheduleAct', para).then(response => {
            _this.spinning = false
            if (response.code === 0) {
              // _this.$emit('init')
              this.showAddTable = false
              _this.$message.success(response.message)
            } else if(response.code === 100001){
              let msgArray = JSON.parse(response.message)
              let msg = "";
              for (let index in msgArray){
                msg = msg + '\n' +msgArray[index]
              }
              alert(msg)
            }else {
              _this.$message.error(response.message)
            }
          })
        }
      })
    },

    /**
     * Function    : cancelApply
     * Description : 取消弹出框
     * Input       : val
     * Return      : none
     */
    cancelApply() {
      this.weekList = []
      this.showAddTable = false
      let _this = this
      setTimeout(function () {
        _this.form.resetFields()
      }, 100)
    },
    initData() {
      this.$store.dispatch('getClassSelectListAct', {})
    }

  },

  created() {
    this.initData()
  }
}
</script>

<style>
.ant-table-thead {
  position: sticky;
  top: 0px;
  z-index: 1;
}
</style>
